package com.qf.shop.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.qf.shop.common.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.qf.shop.user.entity.UserEntity;
import com.qf.shop.user.service.UserService;
import com.qf.shop.common.utils.PageUtils;
import com.qf.shop.common.utils.R;


/**
 * @author Java2103
 * @email Java2103@qq.com
 * @date 2021-07-30 10:32:18
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/findUser")
    public UserEntity findUser(String field, String value) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(field, value);
        return userService.getOne(wrapper);
    }

    /**
     * 列表
     */
    @RequestMapping("/list") // ?id
    // 查询参数如何直接转成一个Map
    public R list(@RequestParam Map<String, Object> params) {
        log.debug("param:{},{}", params, "xxxxxx");
        PageUtils page = userService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user) {
        userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/queryUser")
    public UserVo queryUser(@RequestParam("name") String name) {
        return userService.queryUser(name);
    }
}
